const app = getApp();

Page({
  data: {
    chatList: [],
    viewedCount: 0,
    favoritedCount: 0,
    isLogin: false,
    page:1,
    loading: false,
  },

  onLoad() {
    this.setData({
      isLogin: app.globalData.isLogin
    });
    this.loadChatList(true);
    this.loadCounts();
  },

  onShow() {
    this.setData({
      isLogin: app.globalData.isLogin
    });
    this.loadChatList(true);
    //this.loadCounts();
  },

  // 加载聊天列表
  loadChatList(isRefresh) {
    //if (!app.globalData.isLogin) return;
    if (this.data.loading) return;
    this.setData({ loading: true });
    var _this = this
    wx.request({
        url: app.globalData.baseUrl + 'api/chat-rooms',
        data: {
          page: _this.data.page,
        },
        header: {
            'Authorization': `Bearer ${app.globalData.token}`
        },
        success: function(res) {
          if (res.data.code === 0) {
            const list = res.data.data.needs || []
            const newList = isRefresh ? list : _this.data.chatList.concat(list)
            
            _this.setData({
              chatList: newList,
              hasMore: list.length === 10,
              loading: false
            })
          }
        },
        complete: function() {
            _this.setData({
            loading: false,
          })
        }
    })
    return

    // 模拟API调用
    setTimeout(() => {
      const mockChatList = [
        {
          id: 1,
          userId: 1,
          nickname: '温柔如风',
          age: 58,
          city: '北京市朝阳区',
          avatar: '/images/avatar/11.jpg',
          lastMessage: '你好，很高兴认识你',
          lastTime: '2小时前',
          unreadCount: 2,
          isOnline: true
        },
        {
          id: 2,
          userId: 2,
          nickname: '阳光大叔',
          age: 62,
          city: '北京市海淀区',
          avatar: '/images/avatar/11.jpg',
          lastMessage: '今天天气不错，要不要一起散步？',
          lastTime: '1天前',
          unreadCount: 0,
          isOnline: false
        },
        {
          id: 3,
          userId: 3,
          nickname: '优雅女士',
          age: 55,
          city: '北京市西城区',
          avatar: '/images/avatar/11.jpg',
          lastMessage: '谢谢你的关心',
          lastTime: '3天前',
          unreadCount: 1,
          isOnline: true
        }
      ];
      
      this.setData({
        chatList: mockChatList
      });
    }, 500);
  },

  // 加载计数
  loadCounts() {
    if (!app.globalData.isLogin) return;
    
    // 模拟API调用
    setTimeout(() => {
      this.setData({
        viewedCount: 5,
        favoritedCount: 3
      });
    }, 300);
  },

  // 查看谁看过我
  viewViewedMe() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.navigateTo({
      url: '/pages/viewed-me/viewed-me'
    });
  },

  // 查看谁收藏我
  viewFavoritedMe() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.navigateTo({
      url: '/pages/favorited-me/favorited-me'
    });
  },

  // 查看我看过的
  viewMyViewed() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.navigateTo({
      url: '/pages/my-viewed/my-viewed'
    });
  },

  // 查看我收藏的
  viewMyFavorites() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.navigateTo({
      url: '/pages/my-favorites/my-favorites'
    });
  },

  // 打开聊天
  openChat(e) {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }

    if (!app.globalData.userProfile) {
      wx.showModal({
        title: '提示',
        content: '请先完善个人资料',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/edit-profile/edit-profile'
            });
          }
        }
      });
      return;
    }

    if (!app.globalData.isRealNameAuth) {
      wx.showModal({
        title: '提示',
        content: '请先完成实名认证',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/real-name-auth/real-name-auth'
            });
          }
        }
      });
      return;
    }

    if (!app.globalData.isMember) {
      wx.showModal({
        title: '提示',
        content: '开通会员享受更多聊天功能',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/membership/membership'
            });
          }
        }
      });
      return;
    }

    const id = e.currentTarget.dataset.id;
    var _this = this
    wx.request({
        url: app.globalData.baseUrl + 'api/chat-roomid?obj_user_id='+id,
        data: {
        },
        header: {
          'Authorization': `Bearer ${app.globalData.token}`
        },
        success: res => {
          if (res.data.code === 0) {
              wx.navigateTo({
                 url: `/pages/chat/chat?room_id=${res.data.data.room.id}`
              });
          }
        },
        complete: function() {
            _this.setData({
            loading: false,
          })
        }
    })
  },

  // 显示登录提示
  showLoginTip() {
    wx.showModal({
      title: '提示',
      content: '请先登录',
      success: (res) => {
        if (res.confirm) {
          this.goToLogin();
        }
      }
    });
  },

  // 去登录
  goToLogin() {
    wx.navigateTo({
      url: '/pages/register/register'
    });
  }
}); 